import { Component, OnInit, OnDestroy, Input } from '@angular/core';

import { MonitorAddService } from './monitor.add.component.service';

import { Subscription } from 'rxjs';

import { Util } from '../../../utils/util';

import { AlertsService } from '../../../modules/ng2-alerts';
@Component({
  selector: 'monitor-add',
  templateUrl: './monitor.add.component.html',
  styleUrls: ['./monitor.add.component.css'],
  providers: [MonitorAddService]
})

export class MonitorAddComponent {

  @Input('type') type: string;

  busy: Subscription;

  // pages data
  vm: any = {
    page: 1,
    table: {
      table: [],
      count: 0
    }
  };

  // 参数
  param: any = {
    page: 0,
    start: 0,
    rows: 10,
    sort: 'monthSellMoney',
    sortType: 'desc'
  };

  constructor(private service: MonitorAddService, private util: Util,private _alert: AlertsService) { }

  ngOnInit() {
    console.log(this.type);

    // this.searchKey = '';
  }

  ngOnDestroy() {

    if (this.busy) {
      this.busy.unsubscribe();
    }

  }

  // bing
  keyWords: string = '';

  keyId:string = '';


  searchClear(){
    this.keyWords = '';
  }

  // search
  search() {
    if (this.keyWords == '' && this.keyId =='') {

      this._alert.create('info', '请填写搜索内容', { duration: 2000 });
      return;
    }

    if(this.keyWords !=''){
      this.keyId = '';
      this.param.numberId ='';
      this.param.shopId = '';
    }


    this.getHttp(this.param);
  }


  //分页
  pageChanged(currentPage) {

    console.log(currentPage);

    this.vm.page = currentPage;

    this.param.start = (currentPage - 1) * this.param.rows;

    this.getHttp(this.param);

  }


  // 获取搜索结果
  getHttp(param) {
    switch (this.type) {
      case "product":
        Object.assign(param, { q: this.keyWords,numberId:this.keyId });
        console.log(this.keyWords);

        console.log(param);
        // this.getProductHttp(param);
        break;
      case "shop":
        Object.assign(param, { shopName: this.keyWords,shopId:this.keyId });
        // this.getShopHttp(param);
        break;
      case "brand":
        Object.assign(param, { brandNameSearch: this.keyWords });
        // this.getBrandHttp(param);
        break;

      default:
        // code...
        break;
    }
  }

  getBrandHttp(param) {


    this.busy = this.service.searchBrand(param).subscribe((res: any) => {

      this.vm.table.table = res.data.value;

      this.vm.table.count = res.data.count;

    }, (err: any) => {

    });
  }

  getProductHttp(param) {


    var res = {
      "code":0,"msg":"成功","data":{"value":[
        {"date":"2017-12-03 00:00:00","numberId":"538889418588","title":"【分期免息】Beats BeatsX耳塞式无线蓝牙B耳机入耳式X耳机耳麦","picUrl":"//g-search1.alicdn.com/img/bao/uploaded/i4/i4/1809177149/TB1suYCcx6I8KJjy0FgXXXXzVXa_!!0-item_pic.jpg","loc":"广东深圳","propertyValue":"是否线控:是,品牌:Beats,套餐类型:官方标配,耳机售后服务:全国联保,型号:BeatsX,耳机类型:无线,耳机类别:IOS线控耳机 无线耳机 真无线耳机 运动耳机,生产企业:美国苹果公司,有无麦克风:带麦,兼容平台:iOS,佩戴方式:入耳式,颜色分类:黑色,保修期:12个月","brandName":"beats","price":798.0,"monthPrice":798.0,"quantity":0,"monthSellAmount":24690,"monthSellMoney":1.970262E7,"sellAmount":50,"sellMoney":39900.0,"category":"1205","catName":"数码/手机/家电--影音电器--耳机/耳麦","uid":"1809177149","shopName":"beats官方旗舰店","shopType":"商城","publishTime":null}],"count":1000}};

      this.vm.table.table = res.data.value;

      this.vm.table.count = res.data.count;

    
  }

  getShopHttp(param) {


    this.busy = this.service.searchShop(param).subscribe((res: any) => {

      for(let i in res.data.value){
        let item = res.data.value[i];
        item = this.util.shopLevels(item);
      }

      this.vm.table.table = res.data.value;

      this.vm.table.count = res.data.count;

    }, (err: any) => {

    });
  }

}
